package com.hx.miniblog.controller;

import com.hx.miniblog.pojo.vo.LoginAccountVo;
import com.hx.miniblog.util.utils.JwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wenhao hu
 * @title: BaseController
 * @projectName miniBlog
 * @description: TODO
 * @date 2021/12/2817:28
 */
public class BaseController {

    @Value("${config.jwt.name}")
    private String jwtHeader;
    /**
     * 获取当前登录用户
     */
    public LoginAccountVo currentUser(){
        HttpServletRequest request = request();
        String token = request.getHeader(jwtHeader);
        if (StringUtils.isBlank(token)) {
            token = request.getParameter(jwtHeader);
        }
        return JwtUtil.getLoginVo(token);
    }


    private HttpServletRequest request() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        return requestAttributes.getRequest();
    }
}
